package com.sentiment.database.bean;

import java.util.ArrayList;
import java.util.List;

import org.bson.Document;
import org.bson.types.ObjectId;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.sentiment.bean.OperationInfo;
import com.sentiment.database.dbUtil.DbUtil;

public class Permission  {
	
	@Override
	public String toString() {
		return "Permission [_id=" + _id + ", permissionname=" + permissionname
				+ ", permissiontype=" + permissiontype + ", permissioncode="
				+ permissioncode + ", per_detail=" + per_detail + "]";
	}


	public static final String dbName = "UserDB";
	public static final String collection = "Permission";
	
	private ObjectId _id;
	private String permissionname;
	//权限类型（url/button/...）
	private String permissiontype;
	//权限代码（据此判断是否拥有该权限，唯一，url地址/button名/..）
	private String permissioncode;
	//权限具体解释
	private String per_detail;
	
	public Permission() {
		// TODO Auto-generated constructor stub
	}
	
	public Permission(Document doc){
		super();
		_id = doc.getObjectId("_id");
		permissionname = doc.getString("permissionname");
		permissiontype = doc.getString("permissiontype");
		permissioncode = doc.getString("permissioncode");
		per_detail = doc.getString("per_detail");
		
	}
	
	public Document toDocument(){
		return new Document("permissionname", permissionname).append("permissiontype", permissiontype)
		.append("permissioncode", permissioncode).append("per_detail", per_detail);
	}

	public Permission(String permissionname, String permissiontype, String permissioncode, String per_detail) {
		super();
		this.permissionname = permissionname;
		this.permissiontype = permissiontype;
		this.permissioncode = permissioncode;
		this.per_detail = per_detail;
	}
	public Permission(String id){
		super();
		this._id = new ObjectId(id);
		
	}

	public String get_id() {
		return _id.toString();
	}

	public void set_id(String _id) {
		this._id = new ObjectId(_id);
	}

	public String getPermissionname() {
		return permissionname;
	}

	public void setPermissionname(String permissionname) {
		this.permissionname = permissionname;
	}

	public String getPermissiontype() {
		return permissiontype;
	}

	public void setPermissiontype(String permissiontype) {
		this.permissiontype = permissiontype;
	}

	public String getPermissioncode() {
		return permissioncode;
	}

	public void setPermissioncode(String permissioncode) {
		this.permissioncode = permissioncode;
	}
	
	public String getPer_detail() {
		return per_detail;
	}

	public void setPer_detail(String per_detail) {
		this.per_detail = per_detail;
	}

	/**
	 * 根据权限名称获取 Permission 
	 * @param PermissionName
	 * @return
	 */
	public Permission getPermissionByPermissionName(String PermissionName){
		
		DbUtil db = new DbUtil();
		db.openConnection(dbName, collection);
		
		String[] keys = {"permissionname"};
		Permission per = new Permission(PermissionName,null,null,null);
		Document t = per.toDocument();
		t = db.query(t,keys);
		
		db.closeConnection();
		
		if(t == null)
			return null;
		
		return new Permission(t);
	}
	
	/**
	 * 根据 per_detail 获取 Permission 
	 * @param PermissionName
	 * @return
	 */
	public Permission getPermissionByPermission_detail(String per_detail){
		
		DbUtil db = new DbUtil();
		db.openConnection(dbName, collection);
		
		String[] keys = {"per_detail"};
		Permission per = new Permission(null,null,null,per_detail);
		Document t = per.toDocument();
		t = db.query(t,keys);
		
		db.closeConnection();
		
		if(t == null)
			return null;
		
		return new Permission(t);
	}
	
	/**
	 * 用于测试时向数据库中填写数据
	 */
	public void test() {
		DbUtil perdb = new DbUtil();
		perdb.openConnection(dbName, collection);
		
		Permission per = new Permission();
		Document t = new Document();
//		per = new Permission("contentComp","url","/WebPage/jsp/contentComp.jsp",null);
//		t = per.toDocument();
//		perdb.insert(t);
//		
//		per = new Permission("contentCompTemp","url","/WebPage/jsp/contentCompTemp.jsp",null);
//		t = per.toDocument();
//		perdb.insert(t);
		
		per = new Permission("correlationInfo","url","/WebPage/jsp/correlationInfo.jsp","舆情监测-关联信息查询");
		t = per.toDocument();
		perdb.insert(t);
		
//		per = new Permission("dailySentiment","url","/WebPage/jsp/dailySentiment.jsp","每日舆情报告列表");
//		t = per.toDocument();
//		perdb.insert(t);
		
//		per = new Permission("dailySentimentDetail","url","/WebPage/jsp/dailySentimentDetail.jsp","每日舆情报告详情");
//		t = per.toDocument();
//		perdb.insert(t);
		
		per = new Permission("hotInfoSupervision","url","/WebPage/jsp/hotInfoSupervision.jsp","热点信息监督");
		t = per.toDocument();
		perdb.insert(t);
		
//		per = new Permission("hotSentiment","url","/WebPage/jsp/hotSentiment.jsp","热点舆情报告列表");
//		t = per.toDocument();
//		perdb.insert(t);
		
//		per = new Permission("hotSentimentDetail","url","/WebPage/jsp/hotSentimentDetail.jsp","热点舆情报告详情");
//		t = per.toDocument();
//		perdb.insert(t);
		
		per = new Permission("plagiarismGraph","url","/WebPage/jsp/plagiarismGraph.jsp","图表统计");
		t = per.toDocument();
		perdb.insert(t);
		
		per = new Permission("plagiarismList","url","/WebPage/jsp/plagiarismList.jsp","抄袭检测搜索结果");
		t = per.toDocument();
		perdb.insert(t);
		
		per = new Permission("plagiarismText","url","/WebPage/jsp/plagiarismText.jsp","盗版文本监测");
		t = per.toDocument();
		perdb.insert(t);
		
		per = new Permission("plagiarismUrl","url","/WebPage/jsp/plagiarismUrl.jsp","盗版URL监测");
		t = per.toDocument();
		perdb.insert(t);
		
//		per = new Permission("sentimentGraph","url","/WebPage/jsp/sentimentGraph.jsp","统计图表");
//		t = per.toDocument();
//		perdb.insert(t);
				
		per = new Permission("sentimentList","url","/WebPage/jsp/sentimentList.jsp","热点信息监督搜索结果");
		t = per.toDocument();
		perdb.insert(t);
		
		//后台jsp
		per = new Permission("dailypublish","url","/WebManagement/dailypublish.jsp","每日舆情发布");
		t = per.toDocument();
		perdb.insert(t);
		
//		per = new Permission("frame","url","/WebManagement/frame.jsp",null);
//		t = per.toDocument();
//		perdb.insert(t);
		
		per = new Permission("hotpublish","url","/WebManagement/hotpublish.jsp","热点舆情发布");
		t = per.toDocument();
		perdb.insert(t);
		
		per = new Permission("index","url","/WebManagement/index.jsp","首页");
		t = per.toDocument();
		perdb.insert(t);
		
		per = new Permission("permissionlist","url","/WebManagement/permissionlist.jsp","权限列表");
		t = per.toDocument();
		perdb.insert(t);
		
		per = new Permission("rolelist","url","/WebManagement/rolelist.jsp","角色列表");
		t = per.toDocument();
		perdb.insert(t);
		
		per = new Permission("user","url","/WebManagement/user.jsp","用户个人信息");
		t = per.toDocument();
		perdb.insert(t);
		
		per = new Permission("userlist","url","/WebManagement/userlist.jsp","用户列表");
		t = per.toDocument();
		perdb.insert(t);
//
		per = new Permission("systemedit","url","/WebManagement/systemedit.jsp","系统信息编辑");
		t = per.toDocument();
		perdb.insert(t);
		
		per = new Permission("systemrefer","url","/WebManagement/systemrefer.jsp","系统信息查看");
		t = per.toDocument();
		perdb.insert(t);
		
		per = new Permission("userPaymentsRecord","url","/WebManagement/userPaymentsRecord.jsp","用户缴费历史信息");
		t = per.toDocument();
		perdb.insert(t);
		
		per = new Permission("viewDailyPublishDetail","url","/WebManagement/viewDailyPublishDetail.jsp","每日舆情报告预览");
		t = per.toDocument();
		perdb.insert(t);
		
		per = new Permission("viewUserDetail","url","/WebManagement/viewUserDetail.jsp","查看用户信息详情");
		t = per.toDocument();
		perdb.insert(t);
		
		per = new Permission("historyrecorddaily","url","/WebManagement/historyrecorddaily.jsp","每日舆情历史信息");
		t = per.toDocument();
		perdb.insert(t);
		
		per = new Permission("historyrecordhot","url","/WebManagement/historyrecordhot.jsp","热点舆情历史信息");
		t = per.toDocument();
		perdb.insert(t);
		
		per = new Permission("leftmenu","url","/WebManagement/leftmenu.jsp","菜单");
		t = per.toDocument();
		perdb.insert(t);
		
		per = new Permission("topbar","url","/WebManagement/topbar.jsp","页面顶部信息栏");
		t = per.toDocument();
		perdb.insert(t);
		
		per = new Permission("dailypublishEdit","url","/WebManagement/dailypublishEdit.jsp","编辑每日报告");
		t = per.toDocument();
		perdb.insert(t);
		
		perdb.closeConnection();
		
	}

	/**
	 * 根据权限 id 获取 Permission
	 * @param id
	 * @return
	 */
	public Permission getPermissionByPermissionId(String id) {
		
		DbUtil db = new DbUtil();
		db.openConnection(dbName, collection);
		
		String[] keys = {"_id"};
		Permission per = new Permission(id);
		Document t = per.toDocument_id();
		t = db.query(t,keys);
		
		db.closeConnection();
		
		if(t == null)
			return null;
		
		return new Permission(t);
		
	}
	
	/**
	 * 获取 Permission 表中数据条数
	 * @return
	 */
	@JsonIgnore
	public long getCount(){
		
		DbUtil db = new DbUtil();
		db.openConnection(dbName, collection);
		long count = db.collectionGetCount();
		db.closeConnection();
		return count;
		
	}
	
	/**
	 * 删除当前权限
	 * @return
	 */
	public String deletePermission(){
		
		Permission perdb = new Permission();
		perdb = perdb.getPermissionByPermissionId(this.get_id());
		if(perdb == null){
			return OperationInfo.PermissionNotFound;
		}
		
		//删除 Permission 表中数据
		DbUtil db = new DbUtil();
		db.openConnection(dbName, collection);
		
		String []keys = {"_id"};
		boolean bper = db.remove(this.toDocument_id(), keys);
		
		db.closeConnection();
		
		//删除 Role_Permissions 表中数据
		Role_Permissions role_pers = new Role_Permissions();
		boolean brole_pers = role_pers.deletePermission(perdb);
		
		//删除 User_Permissions 表中数据
		User_Permissions user_pers = new User_Permissions();
		boolean buser_pers = user_pers.deletePermission(perdb);
		
		if(bper && brole_pers && buser_pers)
			return OperationInfo.OperSuccess;
		else
			return OperationInfo.OperFailure;
	}
	
	
	public static void main(String[] args) {
		
		Permission per = new Permission();
		per = per.getPermissionByPermissionId("5629f72f53f2260e882d97c1");
		
		
		System.out.println(per.deletePermission());
		
	}

	private Document toDocument_id() {
		return new Document("_id", _id).append("permissionname", permissionname).append("permissiontype", permissiontype)
				.append("permissioncode", permissioncode).append("per_detail", per_detail);
	}
	
}
